//
//  NetWorkHelper.m
//  test
//
//  Created by mac on 2017/8/31.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "NetWorkHelper.h"

@implementation NetWorkHelper{
    //NSString *BaseUrl;
}

- (instancetype)initWithUrl:(NSString *)url
{
    self = [super init];
    if (self) {
        [PGNetAPIClient baseUrl:url];
    }
    return self;
}

/**
 * get请求，不需要缓存
 */
-(void)GET:(NSString *)url completion:(void (^)(BOOL, NSDictionary *))callback{
    [PGNetworkHelper GET:url parameters:nil cache:false responseCache:nil success:^(NSDictionary *responseObject) {
        callback(true,responseObject);
    } failure:^(NSError *error) {
        if([error code]==-1009){//无网络连接
            callback(false,nil);
        }
    }];
}
/**
 * get请求，自动缓存
 */
-(void)GET:(NSString *)url cacheKey:(NSString *)cacheKey completion:(void (^)(BOOL, NSDictionary *))callback{
    [PGNetworkHelper GET:url parameters:nil cache:false responseCache:nil success:^(NSDictionary *responseObject) {
        [PGNetworkCache saveResponseCache:responseObject forKey:cacheKey];//保存数据
        callback(true,responseObject);
    } failure:^(NSError *error) {
        if([error code]==-1009){//无网络连接
            NSDictionary *responseObject = [PGNetworkCache getResponseCacheForKey:cacheKey];//读取数据
            callback(false,responseObject);
        }
    }];
}

/**
 * post请求，不需要缓存
 */
-(void)POST:(NSString *)url params:(NSDictionary *)params completion:(void (^)(BOOL, NSDictionary *))callback{
    [PGNetworkHelper POST:url parameters:params cache:false responseCache:nil success:^(NSDictionary *responseObject) {
        //回调出去
        callback(true,responseObject);
    } failure:^(NSError *error) {
        if([error code]==-1009){//无网络连接
            callback(false,nil);
        }
    }];
    
    
}
/**
 * post请求，自动缓存
 */
-(void)POST:(NSString *)url cacheKey:(NSString *)cacheKey params:(NSDictionary *)params compile:(void (^)(BOOL success,NSDictionary *data))callback{
    [PGNetworkHelper POST:url parameters:params cache:false responseCache:nil success:^(NSDictionary *responseObject) {
        [PGNetworkCache saveResponseCache:responseObject forKey:cacheKey];//保存数据
        //回调出去
        callback(true,responseObject);
    } failure:^(NSError *error) {
        if([error code]==-1009){//无网络连接
           // NSLog(@"呜呜,你的网了炸掉了");
            NSDictionary *responseObject = [PGNetworkCache getResponseCacheForKey:cacheKey];//读取数据
            callback(false,responseObject);
        }
    }];
}


/**
 * 取消所有请求
 */
-(void)cancelAllOperations{
    [PGNetworkHelper cancelAllOperations];
}

/**
 * 删除指定key的缓存
 */
-(void)removeKeyCache:(NSString *)key{
    [PGNetworkCache removeResponseCacheForKey:key];
}

/**
 * 删除所有的缓存
 */
-(void)removeAllCache{
    [PGNetworkCache removeAllResponseCache];
}


/**
 *  下载文件
 *
 *  @param URL      请求地址
 *  @param fileDir  文件存储目录(默认存储目录为Download)
 *  progress 文件下载的进度信息
 *  success  下载成功的回调(回调参数filePath:文件的路径)
 *  failure  下载失败的回调
 *
 */
-(void)downloadFile:(NSString *)URL fileDir:(NSString *)fileDir{
    [PGNetworkHelper downloadWithURL:URL fileDir:fileDir progress:^(NSProgress *progress) {
        
    } success:^(NSString *filePath) {
        
    } failure:^(NSError *error) {
        
    }];
}
/**
 *  上传图片文件
 *
 *  @param URL        请求地址
 *  @param parameters 请求参数
 *  @param images     图片数组
 *  @param name       文件对应服务器上的字段
 *  @param mimeType   图片文件的类型,例:png、jpeg(默认类型)....
 *  progress   上传进度信息
 *  success    请求成功的回调
 *  failure    请求失败的回调
 *
 *  返回的对象可取消请求,调用cancle方法
 */
-(void)uploadFile:(NSString *)URL parameters:(NSDictionary *)parameters images:(NSArray<UIImage *> *)images name:(NSString *)name mimeType:(NSString *)mimeType{
    [PGNetworkHelper uploadWithURL:URL parameters:parameters images:images name:name mimeType:mimeType progress:^(NSProgress *progress) {
        //上传进度
    } success:^(id responseObject) {
        //上传成功
    } failure:^(NSError *error) {
        //上传失败
    }];
}


/*
 //取消当前网络请求
 NSURLSessionTask *task = [PGNetworkHelper GET:@"api/user/login.json" parameters:nil cache:false responseCache:nil success:^(id responseObject) {
    NSLog(@"responseObject = %@", responseObject);
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
}];
[task cancel]; */


@end
